package org.opens.template;

import org.opens.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 说明:
 * <pre>
 *     1. 自定义一个freemarker标签, 然后就可以在页面上使用这个标签了.
 * </pre>
 */
@Component
public class SidebarDirective extends TemplateDirective {

    @Autowired
    private PersonService personService;

    @Override
    public String getName() {
        return "sidebar";
    }

    /**
     * 说明:
     * <pre>
     *     1. 自定义标签的逻辑方法.
     * </pre>
     */
    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        int size = handler.getInteger("size", 6);
        String method = handler.getString("method", "one");
        switch (method) {
            case "one":
                handler.put(RESULTS, personService.findById(1));
                break;
            case "two":
                handler.put(RESULTS, personService.findById(2));
                break;
            case "three":
                handler.put(RESULTS, personService.findById(3));
                break;
        }
        handler.render();
    }
}
